package com.wish233.wrpcspringbootstarter.boostrap;

import com.wish233.application.RpcApplication;
import com.wish233.config.RpcConfig;
import com.wish233.server.WRpcServer;
import com.wish233.server.impl.VertxNetServer;
import com.wish233.wrpcspringbootstarter.annotation.EnableWrpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author WHH
 * 启动类
 */
@Slf4j
public class RpcInitBootStrap implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean needServer = (boolean) importingClassMetadata.getAnnotationAttributes(EnableWrpc.class.getName()).get("needServer");
        RpcApplication.init();
        final RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        if (needServer) {
            WRpcServer netServer = new VertxNetServer();
            netServer.start(rpcConfig.getServerPort());
        } else {
            log.info("不启动服务器");
        }
    }
}
